/**
 * 
 */
package com.holystone.orca.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

import com.holystone.orca.sys.common.SystemConstant;
import com.holystone.orca.sys.mybatis.model.SysPrivilege;
import com.holystone.orca.sys.mybatis.model.SysRole;
import com.holystone.orca.sys.mybatis.dao.SysPersonMapper;
import com.holystone.orca.sys.mybatis.model.SysPerson;
import com.holystone.orca.util.code.Md5Util;
import com.holystone.orca.sys.model.MenuNavVO;
import com.holystone.orca.sys.model.StaffVO;
import com.holystone.orca.sys.service.ILoginService;
import com.holystone.orca.sys.service.IStaffPrivilegeService;
import com.holystone.orca.sys.service.IStaffRoleService;

/**
 * Copyright (c) 2015, HOLYSTONE Technologies, Inc.
 * All right reserved.
 * @desc:
 * @author:王凯征
 * @date:2015年11月3日 下午5:40:07
 */
@Service(value="loginService")
@Transactional
public class LoginServiceImpl implements ILoginService {
	
	@Autowired
	private SysPersonMapper sysPersonMapper;
	
	@Autowired
	private IStaffPrivilegeService staffPrivilegeService;
	
	@Autowired
	private IStaffRoleService staffRoleService;
	
	/* (non-Javadoc)
	 * @see com.holystone.orca.sys.service.LoginService#isPersonExist(java.lang.String, java.lang.String, java.lang.Long)
	 */
	@Override
	public boolean isPersonExist(String personCode, String personPassword,
			Long key) {
		Example example = new Example(SysPerson.class);
		Example.Criteria ct = example.createCriteria();
		
		if(personCode != null && personCode != ""){
			ct.andEqualTo("personCode", personCode);
		}

		List<SysPerson> pList = sysPersonMapper.selectByExample(example);
		if (pList.size()>0) {
			String pwd=Md5Util.EncoderByMd5(pList.get(0).getPersonPassword()+"-"+key);
			if (pwd.equals(personPassword)) {
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see com.holystone.orca.sys.service.LoginService#initStaffVO(java.lang.String)
	 */
	@Override
	public StaffVO initStaffVO(String personCode) {
		Map<Object, String> map=new HashMap<Object, String>();
		map.put("personCode", personCode);
		//map.put("personPassword",personPassword);
		//map.put("staffState",SystemConstant.STAFF_STATE_A);
		List<StaffVO> pList = sysPersonMapper.getStaffVO(map);
		if(pList!=null && pList.size()==1){
			StaffVO vo = pList.get(0);
			{//初始化员工权限
				List<SysPrivilege> ls=this.staffPrivilegeService.getStaffPrivilegeList(vo.getStaffId());
				vo.setPrivilegeList(ls);
			}
			{//初始化员工角色
				List<SysRole> ls=this.staffRoleService.getStaffRoleList(vo.getStaffId());
				vo.setRoleList(ls);
			}
			return vo;
		}else{
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.holystone.orca.sys.service.LoginService#getDefaultTopMenuRootId(java.util.List)
	 */
	@Override
	public String getDefaultTopMenuRootId(List<SysPrivilege> staffPrivilegeList) {
		List<SysPrivilege> list=new ArrayList<SysPrivilege>();
		for (SysPrivilege privilegePO : staffPrivilegeList) {
			if (SystemConstant.PRI_TYPE_B.equals(privilegePO.getPriType())) {
				list.add(privilegePO);
			}
		}
		String topMenuId=null;
		if (list.size()>0) {
			topMenuId=list.get(0).getPriId().toString();
		}
		return topMenuId;
	}

	/* (non-Javadoc)
	 * @see com.holystone.orca.sys.service.LoginService#getMenuNavVOList(java.util.List)
	 */
	@Override
	public List<MenuNavVO> getMenuNavVOList(List<SysPrivilege> staffPrivilegeList) {
		List<SysPrivilege> ls=staffPrivilegeList;
		for (int i = 0; i < ls.size(); i++) {
			if (null==ls.get(i).getPriPid()) {
				ls.remove(i);
				break;
			}
		}
		List<MenuNavVO> list=new ArrayList<MenuNavVO>();
		//第一层  模块菜单
		for (int i = 0; i < ls.size(); i++) {
			SysPrivilege pv1=ls.get(i);
			MenuNavVO nav1=privielgeToMenu(pv1);
			if (pv1.getPriType().equals(SystemConstant.PRI_TYPE_B)) {//一级菜单
				List<MenuNavVO> list1=new ArrayList<MenuNavVO>();
				//第二次  目录菜单
				for (int j = 0; j < ls.size(); j++) {
					SysPrivilege pv2=ls.get(j);
					MenuNavVO nav2=privielgeToMenu(pv2);
					if (pv2.getPriPid().equals(pv1.getPriId())) {//是父子关系
						List<MenuNavVO> list2=new ArrayList<MenuNavVO>();
						//第三层 操作菜单
						this.buildLeavel3Node(pv2, ls, list2);
						nav2.setMenuList(list2);//组装操作节点
						list1.add(nav2);
					}
				}
				nav1.setMenuList(list1);
				list.add(nav1);
			}
		}
		return list;
	}
	
	//创建操作节点
	private void buildLeavel3Node(SysPrivilege pv,List<SysPrivilege> ls,List<MenuNavVO> list2){
		for (int i = 0; i < ls.size(); i++) {
			SysPrivilege node=ls.get(i);
			if (node.getPriPid().equals(pv.getPriId())) {
				if (node.getPriType().equals(SystemConstant.PRI_TYPE_D)) {
					list2.add(privielgeToMenu(node));
				}
				buildLeavel3Node(node, ls, list2);
			}
		}
	}
	
	public MenuNavVO privielgeToMenu(SysPrivilege privilege){
		MenuNavVO menuNavVO=new MenuNavVO();
		menuNavVO.setPriId(privilege.getPriId().toString());
		menuNavVO.setPriPid(privilege.getPriPid().toString());
		menuNavVO.setPriName(privilege.getPriName());
		menuNavVO.setUrl(privilege.getProgCode());
		menuNavVO.setPriType(privilege.getPriType());
		menuNavVO.setPrior(privilege.getPriPrior());
		menuNavVO.setRemark(privilege.getRemark());
		return menuNavVO;
	}
}
